package com.atguigu.gmall.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 基于延迟插件的java配置类
 */
@Configuration
public class DelayMessageConfig {
    public static final String exchange_delay = "exchange.delay";
    public static final String routing_delay = "routing.delay";
    public static final String queue_delay_1 = "queue.delay.1";

    /**
     * 交换机的类型:
     *  direct: 定向交换机 路由模型  fanout: 扇出交换机 发布订阅模型  topic: 主题交换机 主题模型
     */

    //配置一个基于插件的交换机,需要new 一个CustomExchange自定义的交换机的内容
    @Bean
    public Exchange delayExchange(){
        Map<String, Object> arguments = new HashMap<>();
        //设置交换机的类型
        arguments.put("x-delayed-type","direct");
        //x-delayed-message: 自定义交换机中延迟消息的类型
        return new CustomExchange(exchange_delay,"x-delayed-message",
                true,false,arguments);
    }

    //普通队列
    @Bean
    public Queue delayQueue(){
        return QueueBuilder.durable(queue_delay_1).build();
    }

    //将自定义交换机和普通队列进行绑定
    @Bean
    public Binding bindingDelayExchangeAndDelayQueue(
            @Qualifier(value = "delayExchange") Exchange delayExchange,
            @Qualifier(value = "delayQueue") Queue delayQueue
    ){
        return BindingBuilder.bind(delayQueue).to(delayExchange).with(routing_delay).noargs();
    }
}
